﻿; (function (global) {

    'use strict';

    // pointing pair (row)
    //--9--2374--4-7-59-7--9-4--1861349257-9-7614384378251699--4-7--3-7----94--4829-715
    // 8 in box 2 should be eliminated

    // pointing inside (column)
    //93--5----2--63--95856--2-----318-57---5-2-98--8---5------8--1595-821---4---56---8
    // 3 and 9 in box 5 should be eliminated
    global.Strategies = global.Strategies || {};

    Strategies.PointingPair = function PointingPair() {
        this.type = 'elimination';
    }

    Strategies.PointingPair.prototype.perform = function (grid) {

        var result = this.performByRegion(grid);
        if (result)
            return result;

        return false;
    }

    Strategies.PointingPair.prototype.performByRegion = function (grid) {
        return this.performOnAlignedCells(grid, grid.cellsByRegion);
    }

    Strategies.PointingPair.prototype.performOnAlignedCells = function (grid, regionCellsByRegionId) {
        var containingCells = [];
        for (var i = 0; i < 9; i++) {
            var regionCells = regionCellsByRegionId[i];
            for (var k = 1; k < 10; k++) {
                containingCells.clear();
                var regionId = null;
                var row = null;
                var column = null;
                for (var j = 0; j < 9; j++) {
                    var cell = regionCells[j];
                    if (cell.candidates.has[k]) {
                        if (row === null && column === null) {
                            row = cell.indices.i;
                            column = cell.indices.j;
                        } else {
                            if (row !== cell.indices.i) {
                                row = null;
                            }
                            if (column !== cell.indices.j) {
                                column = null;
                            }
                            if (row === null && column === null)
                                break;
                        }
                        containingCells.push(cell);
                    }
                }

                if ((row === null && column === null) || containingCells.length === 1)
                    continue;

                if (row != null) {
                    var result = SolvingHelper.removeFromOtherCells(k, containingCells, grid.cells[row])
                    if (result) {
                        //console.log('pointing inside', k, regionIdOrNull);
                        return { candidate: k, sourceRegionId: i, containingCells: containingCells, targetCells: grid.cells[row], mode: 'row' };
                    }
                }

                if (column != null) {
                    var result = SolvingHelper.removeFromOtherCells(k, containingCells, grid.cellsByColumn[column])
                    if (result) {
                        //console.log('pointing inside', k, regionIdOrNull);
                        return { candidate: k, sourceRegionId: i, containingCells: containingCells, targetCells: grid.cellsByColumn[column], mode: 'column' };
                    }
                }
            }
        }
        return null;
    }

}(this));
